package com.kk.wish.activity;

import java.util.concurrent.TimeUnit;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kk.wish.R;
import com.kk.wish.view.MarqueeTextView;

public class Loading extends Activity {
	
	private ProgressBar loading;
	private TextView loadingTitle;
	private TextView progressTxt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loading);
		
		loading = (ProgressBar) findViewById(R.id.loadingBar);
		loadingTitle = (TextView) findViewById(R.id.loadingTitle);
		progressTxt = (TextView) findViewById(R.id.progressTxt);
		MarqueeTextView mt = (MarqueeTextView) findViewById(R.id.loadingMarquee);
		mt.init(this);
		mt.startScroll();
		
		
		new AsyncTask<Void, Integer, Void>() {
			int progress = 0;
			@Override
			protected Void doInBackground(Void... params) {
				while (progress < loading.getMax()) {
					publishProgress(progress++);
					try {
						TimeUnit.MILLISECONDS.sleep(50);
//						if (progress % 23 == 0) {
//							TimeUnit.MILLISECONDS.sleep(2500);
//						}
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				return null;
			}
			@Override
			protected void onProgressUpdate(Integer... values) {
				loading.setProgress(progress);
				progressTxt.setText(progress + "%");
				if (loading.getMax() == progress) {
					loadingTitle.setText(R.string.loading_complete);
				}
			}
			
			protected void onPostExecute(Void result) {
				Intent intent = new Intent();
				intent.setClass(Loading.this, Login.class);
				startActivity(intent);
				overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
				finish();
			}
		}.execute();
	}
}
